﻿Imports DevComponents.AdvTree
Imports GenericDescriptionLibrary

Public Class frmAdmin

    Public Event NodeSelected(ByVal aNode As Node)

    Private _ImageInformation As New ImageInformation(My.Settings.ImagePath)

    Private Sub frmAdmin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DevComponents.DotNetBar.RibbonPredefinedColorSchemes.ChangeOffice2007ColorTable(Me, My.Settings.UIColor)
        ShowAll()
    End Sub

    Private Sub ButtonItemRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItemRefresh.Click
        ShowAll()
    End Sub

    Private _Ojects As Objects
    Private Property Objects() As Objects
        Get
            Dim myXMLFilePath As String = My.Settings.PathToXMLFile
            Dim myObjects As Objects = _Ojects
            If myObjects Is Nothing Then
                myObjects = GenericDescriptionLibrary.DemoData.ReadDataFromXML(myXMLFilePath)
                If myObjects Is Nothing Then
                    myObjects = GenericDescriptionLibrary.DemoData.CreateDemoData()
                    GenericDescriptionLibrary.DemoData.SaveDataAsXML(myObjects, myXMLFilePath)
                End If
                _Ojects = myObjects
            End If
            Return myObjects
        End Get
        Set(ByVal value As Objects)
            _Ojects = value
        End Set
    End Property


    Private Sub ShowAll()

        'create the root
        Dim myRootNode As Node = CreateObjectTypeTemplateColumns()
        'loop over the children
        Dim myObjects As Objects = Objects
        For Each myObjectTypeTemplate As ObjectTypeTemplate In myObjects.DictionaryOfObjectTypeTemplates.Values
            CreateObjectTypes(myRootNode, myObjectTypeTemplate)
        Next

        AdvTree1.SuspendLayout()
        'clear all exiting items
        AdvTree1.Nodes.Clear()
        'add the new items
        AdvTree1.Nodes.Add(myRootNode)
        AdvTree1.ResumeLayout()
    End Sub

    Private Sub CreateObjectTypes(ByVal aParentNode As Node, ByVal anObjectTypeTemplate As ObjectTypeTemplate)
        Dim myNode As New Node()
        With myNode
            .Name = anObjectTypeTemplate.ID.ToString
            .DataKey = anObjectTypeTemplate
            .Cells.Clear()
            .Cells.Add(CreateTextWithImageCell(anObjectTypeTemplate.Name, anObjectTypeTemplate.ImageName, eCellPartAlignment.NearCenter, anObjectTypeTemplate.ID))
            '.Cells.Add(CreateImageCell(anObjectTypeTemplate.ImageName, anObjectTypeTemplate.ID))
            .Cells.Add(CreateTextCell(anObjectTypeTemplate.VisioShapeName, anObjectTypeTemplate.ID))
            .Expand()
        End With
        'Create Properties
        Dim myNodeProperties As Node = CreateaPropertyColumns(myNode, "Properties:")
        For Each myP As ObjectTypePropertyTemplate In anObjectTypeTemplate.ListOfProperties
            CreatePropertyTemplateItem(myNodeProperties, myP)
        Next

        'create Relation
        Dim myNodeRelations As Node = CreateaRelationTemplateColumns(myNode, "Relations:")
        'do the children
        For Each myR As Relation In anObjectTypeTemplate.ListOfRelations
            'Create the Children
            CreateRelationItems(myNodeRelations, myR)
        Next
        aParentNode.Nodes.Add(myNode)
    End Sub

#Region "Cells"
    Private Sub CreateRelationItems(ByVal aParentNode As Node, ByVal aRelation As Relation)
        Dim myNode As New Node
        With myNode
            .Name = aRelation.ID.ToString
            .DataKey = aRelation
            .Cells.Clear()
            Dim myOTT As ObjectTypeTemplate = Nothing
            Dim myKey As String = aRelation.ObjectTypeTemplate_ID.ToString
            If Objects.DictionaryOfObjectTypeTemplates.ContainsKey(myKey) Then myOTT = Objects.DictionaryOfObjectTypeTemplates.Item(aRelation.ObjectTypeTemplate_ID.ToString)
            Dim myObjectTypeTemplate As ObjectTypeTemplate = Objects.DictionaryOfObjectTypeTemplates.Item(aRelation.ObjectTypeTemplate_ID.ToString)

            .Cells.Add(CreateTextCell(myObjectTypeTemplate.Name, myObjectTypeTemplate.ID))
            .Cells.Add(CreateTextCell(aRelation.RelationType.ToString, aRelation.ID))
        End With
        aParentNode.Nodes.Add(myNode)
    End Sub

    Private Sub CreatePropertyTemplateItem(ByVal aParentNode As Node, ByVal aProperty As ObjectTypePropertyTemplate)
        Dim myNode As New Node
        With myNode
            .Name = aProperty.ID.ToString
            .DataKey = aProperty
            .Cells.Clear()
            .Cells.Add(CreateTextCell(aProperty.Name, aProperty.ID))
            .Cells.Add(CreateTextCell(aProperty.Datatype, aProperty.ID))
            .Cells.Add(CreateTextCell(aProperty.FieldDefinition.ToString, aProperty.ID))
            .Cells.Add(CreateBooleanCell(aProperty.EmptyAllowed, aProperty.ID))
            .Cells.Add(CreateTextCell(aProperty.SortOrder, aProperty.ID))
            .Cells.Add(CreateBooleanCell(aProperty.IsSystem, aProperty.ID))
        End With
        aParentNode.Nodes.Add(myNode)
    End Sub
#End Region

#Region "Columns"
    Private Function CreateObjectTypeTemplateColumns() As Node
        'create a simple node
        Dim myNode As New Node
        With myNode
            .Name = "nodeRoot"
            .Text = "ObjectType"
            .NodesColumns.Clear()
            .NodesColumns.Add(CreateHeaderColumn("Name:", "OTCH_Name"))
            .NodesColumns.Add(CreateHeaderColumn("Shape Name", "OTCH_VisioShapeName"))
            .Expand()
        End With
        Return myNode
    End Function

    Private Function CreateaPropertyColumns(ByVal aParentNode As Node, ByVal aText As String) As Node
        Dim myNode As New Node
        With myNode
            .Text = aText
            .NodesColumns.Clear()
            .NodesColumns.Add(CreateHeaderColumn("Name:", "PTCH_Name"))
            .NodesColumns.Add(CreateHeaderColumn("Datatype:", "PTCH_Datatype"))
            .NodesColumns.Add(CreateHeaderColumn("Fielddefinition:", "PTCH_FieldDefinition"))
            .NodesColumns.Add(CreateHeaderColumn("Empty Allowed", "PTCH_EmptyAllowed"))
            .NodesColumns.Add(CreateHeaderColumn("SortOrder", "PTCH_Sortorder"))
            .NodesColumns.Add(CreateHeaderColumn("Is System", "PTCH_IsSystem"))
            .Expand()
        End With
        aParentNode.Nodes.Add(myNode)
        Return myNode
    End Function

    Private Function CreateaRelationTemplateColumns(ByVal aParentNode As Node, ByVal aText As String) As Node
        Dim myNode As New Node
        With myNode
            .Text = aText
            .NodesColumns.Clear()
            .NodesColumns.Add(CreateHeaderColumn("ObjectTypeTemplate:", "RTCH_ObjectTypeTemplate"))
            .NodesColumns.Add(CreateHeaderColumn("RelationType", "RTCH_RelationType"))
            .Expand()
        End With
        aParentNode.Nodes.Add(myNode)
        Return myNode
    End Function
#End Region

    Private Function CreateTextWithImageCell(ByVal aText As String, ByVal anImageName As String, ByVal anImageAlignment As eCellPartAlignment, ByVal anID As Guid) As Cell
        Dim myCell As Cell = CreateTextCell(aText, anID)
        With myCell
            .ImageAlignment = anImageAlignment
            Dim mySize As Size = My.Settings.TreeViewImageSize
            Dim myImage As Image = _ImageInformation.GetImage(anImageName, mySize)
            .Images.Image = myImage
        End With
        Return myCell
    End Function

    Private Function CreateTextCell(ByVal aText As String, ByVal anID As Guid) As Cell
        Dim myCell As New Cell
        With myCell
            .Text = aText
            .Name = anID.ToString
        End With
        Return myCell
    End Function

    Private Function CreateImageCell(ByVal anImageName As String, ByVal anID As Guid) As Cell
        Dim myCell As New Cell
        With myCell
            Dim mySize As Size = My.Settings.TreeViewImageSize
            Dim myImage As Image = _ImageInformation.GetImage(anImageName, mySize)
            'myImage = _ImageInformation.GetImage(anImageName)
            .Images.Image = myImage
            .Name = anID.ToString
        End With
        Return myCell
    End Function

    Private Function CreateBooleanCell(ByVal aValue As Boolean, ByVal anID As Guid) As Cell
        Dim myCell As New Cell
        With myCell
            .CheckBoxVisible = True
            .Checked = aValue
            .Name = anID.ToString
        End With
        Return myCell
    End Function

    Private Function CreateHeaderColumn(ByVal aHeaderText As String, ByVal aColumnName As String, Optional ByVal anAutoSizeThisColumn As Boolean = False, Optional ByVal anAbsolutWidth As Integer = 150) As ColumnHeader
        Dim myColumnHeader As New ColumnHeader(aHeaderText)
        With myColumnHeader
            .DoubleClickAutoSize = True
            .Width.AutoSize = anAutoSizeThisColumn
            .Width.Absolute = anAbsolutWidth
        End With
        Return myColumnHeader
    End Function

End Class